package com.sxxm.qxgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxxm.qxgl.dto.Userdto;
import com.sxxm.qxgl.entity.SysUser;
import com.sxxm.qxgl.enums.IsDeletedEnum;
import com.sxxm.qxgl.enums.StatusEnum;
import com.sxxm.qxgl.mapper.SysUserMapper;
import com.sxxm.qxgl.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxxm.qxgl.utils.DataCheckUtils;
import com.sxxm.qxgl.utils.TokenUtils;
import com.sxxm.qxgl.vo.Result;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author qjw
 * @since 2021-09-14
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    public SysUserMapper sysUserMapper;

    public SysUserServiceImpl(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public Result longin(String account, String password) {
        //数据校验
        DataCheckUtils.isNotBlank(account, "用户账号不能为空!");
        DataCheckUtils.isNotBlank(password, "密码不能为空!");
//        DataCheckUtils.isNotBlank(userLoginBo.getCode(), "验证码不能为空!");
        //以用户类型与账号查询用户信息
        QueryWrapper<SysUser> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.lambda().eq(SysUser::getAccount, account).eq(SysUser::getIsDeleted, IsDeletedEnum.NO);
        SysUser user = sysUserMapper.selectOne(sectionQueryWrapper);

        if (user == null) {
            return Result.error().message("账号不存在！");
        }
        if (StatusEnum.INVALID.getKey().equals(user.getStatus())) {
            return Result.error().message("用户已停用！");
        }
        System.out.println("vsdgvr===================");
        Userdto userdto = new Userdto();
        userdto.setName(account);
        userdto.setPassword(password);
        HashMap<String, Object> map = new HashMap<>();

        map.put("account", account);
        map.put("password", password);
        List<SysUser> user1 = sysUserMapper.selectByMap(map);
        //查询结果校验

//        SysUser user = sysUserMapper.login(userdto);
        if (user1 == null) {
            return Result.error().message("用户名或密码错误");
        }
        return Result.ok().message("登录成功").data(TokenUtils.getJwtToken(user1.get(0).getId().toString()));
    }

    @Override
    public Result cx(Long id ,String account, String mobile, String email, String name, int pag, int sl) {
//        DataCheckUtils.isNotBlank(pag, "用户账号不能为空!");
//        DataCheckUtils.isNotBlank(sl, "密码不能为空!");
//        DataCheckUtils.isNotBlank(email, "密码不能为空!");

        Page<SysUser> page = new Page<>(pag, sl);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like("account", account)
                .like("mobile", mobile)
                .like("name", name)
                .like("email", email);
        if (id != null) {
            queryWrapper.eq("id", id);
        }


//        System.out.println(users);
        return Result.ok().message("成功").data(sysUserMapper.selectMapsPage(page, queryWrapper));
    }

    @Override
    public Result ModifyThe(SysUser sysUser) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getId, sysUser.getId());
        //修改条件
        SysUser user = sysUserMapper.selectOne(queryWrapper);

        sysUser.setId(user.getId());
        int a = sysUserMapper.updateById(sysUser);
        if (a <= 0) {
            return Result.error().message("修改失败");
        }
        return Result.ok().message("修改成功");
    }

    @Override
    public Result Addition(SysUser sysUser) {
        if (sysUserMapper.insert(sysUser) <= 0) {
            return Result.error().message("添加失败");
        }
        return Result.ok().message("添加成功");

    }

    @Override
    public Result Delete(long id) {

        if (sysUserMapper.deleteById(id) <= 0) {
            return Result.error().message("删除失败");
        }
        return Result.ok().message("删除成功");
    }

    @Override
    public Result zt(long id) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getId, id);
        //修改条件
        SysUser user = sysUserMapper.selectOne(queryWrapper);
        SysUser user1 = new SysUser();
        user1.setId(user.getId());
        if (sysUserMapper.selectById(id).getStatus() == 0) {
            user1.setStatus(1);
        } else {
            user1.setStatus(0);
        }
        if (sysUserMapper.updateById(user1) <= 0) {
            return Result.error().message("状态修改失败");
        }
        return Result.ok().message("状态修改成功");
    }
}
